package com.lpc.android.abc.main.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lpc.android.abc.R;
import com.lpc.android.abc.base.BuildConfig;
import com.lpc.android.abc.base.base.BaseApplication;
import com.lpc.android.abc.base.base.BaseFragment;
import com.lpc.android.abc.base.bean.Campaign;
import com.lpc.android.abc.base.bean.HomeCampaign;
import com.lpc.android.abc.base.constants.Constants;
import com.lpc.android.abc.base.utils.LogUtil;
import com.lpc.android.abc.base.widget.TopNavBar;
import com.lpc.android.abc.main.activity.WareListActivity;
import com.lpc.android.abc.main.adapter.HomeCatgoryAdapter;
import com.lpc.android.abc.main.di.component.DaggerMainComponent;
import com.lpc.android.abc.main.di.module.MainModule;
import com.lpc.android.abc.main.mvp.MainContract;
import com.lpc.android.abc.main.mvp.MainPresenter;
import com.vlonjatg.progressactivity.ProgressActivity;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.Unbinder;

/**
 * Created by lipc0113 on 2017/6/21 0021.
 */

public class HomeFragment extends BaseFragment<MainPresenter> implements MainContract.HomeInfoView {

    @BindView(R.id.progressActivity)
    ProgressActivity progressActivity;
    @BindView(R.id.toolbar)
    TopNavBar toolbar;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.refreshLayout)
    TwinklingRefreshLayout refreshLayout;

    private Unbinder unbinder;
    private HomeCatgoryAdapter mAdatper;

    @Inject
    MainPresenter mPresenter;


    @Override
    protected int setFragmentLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        DaggerMainComponent.builder().appComponent(BaseApplication.getAppComponent())
                .mainModule(new MainModule()).build().inject(this);

//        mPresenter = MainPresenter.getInstance();
        mPresenter.setHomeInfoView(this);

        refreshLayout.setEnableLoadmore(false);
        refreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(final TwinklingRefreshLayout refreshLayout) {

                mPresenter.getHomeInfo();
            }

            @Override
            public void onLoadMore(final TwinklingRefreshLayout refreshLayout) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        refreshLayout.finishLoadmore();
                    }
                }, 2000);
            }
        });

        progressActivity.showLoading();
        refreshLayout.startRefresh();
//        mPresenter.getHomeInfo();
    }

    @Override
    public void homeInfoSuccess(List<HomeCampaign> homeCampaigns) {

        refreshLayout.finishRefreshing();
        if (homeCampaigns == null || homeCampaigns.size() <= 0) {
            progressActivity.showEmpty(getResources().getDrawable(R.drawable.load_fail)
                    , getString(R.string.prompt), getString(R.string.no_data));
            return;
        }

        mAdatper = new HomeCatgoryAdapter(this, homeCampaigns);

        mAdatper.setOnCampaignClickListener(new HomeCatgoryAdapter.OnCampaignClickListener() {
            @Override
            public void onClick(View view, Campaign campaign) {


                Intent intent = new Intent(getActivity(), WareListActivity.class);
                intent.putExtra(Constants.COMPAINGAIN_ID, campaign.getId());

                startActivity(intent);

            }
        });

        recyclerview.setAdapter(mAdatper);
        recyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));

        progressActivity.showContent();
    }

    @Override
    public void homeInfoFail(String fail) {

        refreshLayout.finishRefreshing();
        progressActivity.showError(getResources().getDrawable(R.drawable.load_fail),
                getString(R.string.prompt), getString(R.string.load_fail), getString(R.string.load_again),
                new ProgressActivity.OnClickListener() {


                    @Override
                    public void onClick(View v) {
                        progressActivity.showLoading();
                        mPresenter.getHomeInfo();
                    }
                });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mPresenter.setHomeInfoView(null);
    }

}
